package com.ah.hf.controller;

import com.ah.hf.common.R;
import com.ah.hf.entity.Category;
import com.ah.hf.entity.Dish;
import com.ah.hf.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.log.LogInputStream;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @PostMapping
    public R<String> addFood(@RequestBody Category category){
        log.info("添加菜品");

        boolean save = categoryService.save(category);
        if (save) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @GetMapping("/page")
    public R<Page> getPage(int page, int pageSize){
        log.info("查询数据 : {}  {}",page ,pageSize);
        //分也构造器
        Page<Category> pageInfo = new Page<>(page ,pageSize);
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        //升序排序
        qw.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,qw);
        return R.success(pageInfo);
    }
    //修改菜品、套餐
    @DeleteMapping
    public R<String> deleteCategory(Long ids){
        log.info("ids: {}" ,ids);
        //执行自定义remove方法
        categoryService.remove(ids);
        //没有异常，直接抛出
        return R.success("删除成功");
    }
    //修改菜品、套餐
    @PutMapping
    public R<String> Modify(@RequestBody Category category){
        log.info("修改分类信息 : {}",category);
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(category.getType() != null,Category::getType,category.getType());

        //根据sort排序
        qw.orderByAsc(Category::getSort).
                //sort排序相等，根据updateTime排序
                orderByDesc(Category::getUpdateTime);
        
        List<Category> list = categoryService.list(qw);

        return R.success(list);
    }
}
